package his.controller;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http;
import his.entity.FmedItem;
import his.entity.User;
import his.service.FmedItemService;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/no_medicine_pro/")
public class FmedItemController {

    @Autowired
    FmedItemService fmedItemService;


    @GetMapping("all")
    @ResponseBody
    public CommonResponse<List<FmedItem>> getall_fmeditems()
    {
        return  fmedItemService.getall_fmeditems();
    }

    @PostMapping("add")
    @ResponseBody
    public CommonResponse<List<FmedItem>> add_fmeditems(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String itemCode=(String) jsonObject.get("itemCode");
        String itemName=(String) jsonObject.get("itemName");
        String format=(String) jsonObject.get("format");
        BigDecimal price= new BigDecimal((String) jsonObject.get("price")) ;
        int expClassID=Integer.parseInt((String) jsonObject.get("expClassID"));
        int deptID=Integer.parseInt((String) jsonObject.get("deptID"));
        String mnemonicCode=(String) jsonObject.get("mnemonicCode");

        int recordType=Integer.parseInt((String) jsonObject.get("recordtype"));
        return  fmedItemService.add_fmeditems(itemCode,itemName,format,price,expClassID,deptID,mnemonicCode,recordType);
    }


    @PostMapping("modify/{item_code}")
    @ResponseBody
    public CommonResponse<List<FmedItem>> update_fmeditems(@PathVariable String item_code,HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String itemCode=(String) jsonObject.get("itemCode");
        String itemName=(String) jsonObject.get("itemName");
        String format=(String) jsonObject.get("format");
        BigDecimal price= (BigDecimal) jsonObject.get("price") ;
        int expClassID=(int) jsonObject.get("expClassID");
        int deptID=(int) jsonObject.get("deptID");
        String mnemonicCode=(String) jsonObject.get("mnemonicCode");

        int recordType=(int) jsonObject.get("recordtype");
        return  fmedItemService.update_fmeditems(item_code,itemName,format,price,expClassID,deptID,mnemonicCode,recordType);
    }

    @GetMapping("search/{keyword}")
    @ResponseBody
    public CommonResponse<List<FmedItem>> getall_fmeditems_bycodename(@PathVariable String keyword)
    {
        return  fmedItemService.getall_fmeditems_bycodename(keyword);
    }

    @DeleteMapping("del/{item_code}")
    @ResponseBody
    public CommonResponse<List<FmedItem>> delete_fmeditems(@PathVariable String item_code)
    {
        return  fmedItemService.delete_fmeditems(item_code);
    }

}
